package com.fenghongzhang.day016;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MusicAdapter extends BaseAdapter {

    private Context context;
    //把音乐封装起来,一些都是对象
    private List<Music> list;

    public MusicAdapter(Context context, List<Music> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.music_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.img = convertView.findViewById(R.id.img);
            viewHolder.title = convertView.findViewById(R.id.title);
            viewHolder.artist = convertView.findViewById(R.id.artist);
            viewHolder.duration = convertView.findViewById(R.id.duration);
            convertView.setTag(viewHolder);

        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Music music = list.get(position);
        viewHolder.artist.setText(music.getArtist());
        viewHolder.title.setText(music.getTitle());
        viewHolder.duration.setText(MusicUtil.fmtTime(music.getDuration()));
        return convertView;
    }
    //布局中的字段-组件
    class ViewHolder{
        ImageView img;
        TextView title;
        TextView artist;
        TextView duration;

    }
}
